package ar.com.fracervellera.model;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.joda.time.DateTime;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;

@PersistenceCapable
public class Project {

	@Persistent
	private String title;
	@Persistent
	private String description;
	@Persistent
	private BlobKey webContent;
	@Persistent
	private BlobKey downloadContent;
	@PrimaryKey
	@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
	private Key key;
	@Persistent
	private long createDate;
	@Persistent
	private String contentType;
	@Persistent
	private boolean showAtHome;
	@Persistent
	private String category;
	
	public Project(String title, String description, BlobKey webContent, BlobKey downloadContent, String contentType) {
		super();
		setTitle(title);
		setDescription(description);
		setWebContent(webContent);
		setDownloadContent(downloadContent);
		setContentType(contentType);
	}
	
	public Project(String title, String description, BlobKey webContent, BlobKey downloadContent,String contentType, long date) {
		this(title,description,webContent, downloadContent, contentType);
		setCreateDate(date);
	}

	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		if(title != null && !title.isEmpty()){
			this.title = title;			
		}
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		if(description != null && !description.isEmpty()) {
			this.description = description;
		}
	}
	public void setWebContent(BlobKey blobKey) {
		if(blobKey != null) {
			this.webContent = blobKey;
		}
	}
	public BlobKey getWebContent() {
		return webContent;
	}
	public void setKey(Key key) {
		if(key != null) {
			this.key = key;
		}
	}
	public Key getKey() {
		return key;
	}

	public void setCreateDate(long createDate) {
		this.createDate = createDate;
	}

	public long getCreateDate() {
		return createDate;
	}
	
	public JSONObject toJson() {
		JSONObject ans = new JSONObject();
			try {
				ans.put("Title", getTitle());
				ans.put("blobKey", getWebContent().getKeyString());
				ans.put("key", KeyFactory.keyToString(getKey()));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		return ans;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getContentType() {
		return contentType;
	}
	 public String getWebContentPath() {
		 return getWebContent().getKeyString();
	 }

	public void setDownloadContent(BlobKey downloadContent) {
		if(downloadContent != null) {
			this.downloadContent = downloadContent;
		}
	}

	public BlobKey getDownloadContent() {
		return downloadContent;
	}

	public void setShowAtHome(boolean showAtHome) {
		this.showAtHome = showAtHome;
	}

	public boolean isShowAtHome() {
		return showAtHome;
	}

	public void setCategory(ProjectCategory category) {
		this.category = category.getCategory();
	}

	public String getCategory() {
		return category;
	}
	
}
